#include "../core/engine/interface.h"
#include "window.h"

namespace sleek
{
    namespace gui
    {
        window::window(core::engine::interface *m) : frame(m)
        {
            title_size = 20;
            isHovored = isMoved = false;

            close = new button(m);
            close->box = core::math::aabbox2di(core::math::vector2di(0,0),core::math::vector2di(10,10));
            close->relative = core::math::vector2di(box.getSize().width-15, 5);
            close->parent = this;
            close->UpdateAbsolutePosition();
            close->setText("Close");
        }
        window::~window()
        {
        }
        button* window::getCloseButton()
        {
            return close;
        }
        void window::UpdateFontPos()
        {
            textpos = absolute+relative;
            textpos.height += ft2->getBoxSize().height+7;
            textpos.width += 12;
        }
        void window::setScale(core::math::vector2di i)
        {
            frame::setScale(i);
            close->relative = core::math::vector2di(box.getSize().width-15, 5);
            close->UpdateAbsolutePosition();
        }
        void window::move(core::math::vector2di i)
        {
            frame::move(i);
            close->relative = core::math::vector2di(box.getSize().width-15, 5);
            close->UpdateAbsolutePosition();
        }
        bool window::manage(core::device::input *e)
        {
            bool CHovored = isHovored, CMoved = isMoved;
            if(close->manage(e) && e->gui.code == IET_BUTTON_CLICKED){ mom->removeFrame(this); return true; }
            if(e->type == core::device::EVENT_MOUSSE_DOWN && e->mouse[core::device::MOUSE_LEFT] &&  box.intersect(e->mouse_pos))
            {
                mom->unActiveElement();
                isActive = true;
            }
            if(frame::manage(e)) { return false; }

            core::math::aabbox2di til(box.getUpperLeft(), core::math::vector2di(box.getUpperLeft().width+box.getSize().width, (int)box.getUpperLeft().height+title_size));
            if(e->type == core::device::EVENT_MOUSSE_DOWN && e->mouse[core::device::MOUSE_LEFT] && til.intersect(e->mouse_pos)) isMoved = true;
            if(e->type == core::device::EVENT_MOUSSE_DOWN && e->mouse[core::device::MOUSE_LEFT] && box.intersect(e->mouse_pos)) mom->popFrame(this);
            if(e->type == core::device::EVENT_MOUSSE_UP) isMoved = false;
            if(e->type == core::device::EVENT_MOUSSE_MOVED)
            {
                if(!isMoved)
                {
                    isHovored = box.intersect(e->mouse_pos);
                    def = e->mouse_pos-box.getUpperLeft();
                }
                else
                {
                    relative = core::math::vector2di();
                    absolute = e->mouse_pos-def;
                    UpdateAbsolutePosition();
                    close->UpdateAbsolutePosition();
                    UpdateFontPos();
                }
            }

            if(isMoved){ e->gui.called = this; e->gui.code = gui::IET_WINDOW_MOVED; return true; }
            if(CHovored != isHovored)
            {
                e->gui.called = this;
                if(isHovored) { e->gui.code = gui::IET_WINDOW_HOVORED; return true; }
                else {  e->gui.code = gui::IET_WINDOW_FOCUSED; return true; }
            }
            return false;
        }
        void window::render()
        {
            if(!show) return;
            if(isMoved)
            {
                mom->getTheme()->drawWindowMainMoved(this);
                mom->getTheme()->drawWindowDecorationMoved(this);
                mom->getTheme()->drawWindowTitleMoved(this);
            }
            else if(isHovored)
            {
                mom->getTheme()->drawWindowMainHovored(this);
                mom->getTheme()->drawWindowDecorationHovored(this);
                mom->getTheme()->drawWindowTitleHovored(this);
            }
            else
            {
                mom->getTheme()->drawWindowMain(this);
                mom->getTheme()->drawWindowDecoration(this);
                mom->getTheme()->drawWindowTitle(this);
            }
            if(ft2) mom->getTheme()->drawFont(this);
            close->render();
            renderChild();
        }
    }
}
